package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.DailyNews;
import cn.wolfcode.trip.base.domain.NewsContent;
import cn.wolfcode.trip.base.query.DailyNewsQuery;
import cn.wolfcode.trip.base.service.IDailyNewsService;
import cn.wolfcode.trip.base.util.JSONResult;
import cn.wolfcode.trip.base.util.UploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

/**
 * admin的日报控制器
 */
@Controller
@RequestMapping("dailyNews")
public class DailyNewsController {

    @Autowired
    private IDailyNewsService newsService;


    //分页方法
    @RequestMapping("list")
    public String list(@ModelAttribute("qo") DailyNewsQuery qo, Model model){
        model.addAttribute("pageInfo",newsService.queryForList(qo));

        return "dailyNews/list";
    }

    //修改或者新增方法
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(MultipartFile file ,DailyNews entity) {
        if (file!=null&&file.getSize()>0){
            String upload = UploadUtil.upload(file, UploadUtil.PATH + "/upload");
            entity.setCoverImgUrl(upload);
        }
        newsService.saveOrUpdate(entity);
        return new JSONResult(); //象征性返回操作成功
    }


    @RequestMapping("getContentById")
    @ResponseBody
    public NewsContent getContentById(Long id) {
        return newsService.getContentById(id);
    }
}
